package com.itheima.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.common.R;
import com.itheima.common.pageParm;
import com.itheima.dao.SetmealDao;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import com.itheima.service.dishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private dishService dishService;
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto) {


        int row = setmealService.add(setmealDto);

        if (row > 0) {
            return R.success("添加套餐成功！");
        } else {
            return R.error("添加套餐失败！");
        }


    }


    @GetMapping("/page")
    public R findPage(pageParm pageParm) {

        IPage<SetmealDto> iPage = setmealService.findPage(pageParm);

        return R.success(iPage);

    }


    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto) {
        int row = setmealService.update(setmealDto);

        if (row > 0) {
            return R.success("更新成功！");
        } else {
            return R.error("更新失败！");
        }
    }

    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {


        int row = setmealService.delete(ids);

        if (row > 0) {
            return R.success("删除套餐成功！");
        } else {
            return R.error("删除套餐失败！");
        }
    }

    @GetMapping("/list")
    public R findByCategoryId(long categoryId, Integer status) {

        List<Setmeal> list = setmealService.findByCategoryId(categoryId, status);


        return R.success(list);
    }

    @GetMapping("/dish/{setmealId}")
    public R findBydishId(@PathVariable long setmealId){


        List<SetmealDish> setmealDishList = setmealDishService.findBySetmealId(setmealId);

        return R.success(setmealDishList);




    }


    @PostMapping("/status/{status}")
    public R updateStatus(@RequestParam List<Long> ids,@PathVariable Integer status){
        int row = setmealService.updateStatus(ids,status);

        if (row > 0) {
            return R.success("更新套餐成功！");
        } else {
            return R.error("更新套餐失败！");
        }
    }
}
